/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>Project: javaseapp - T8
 * <p>Powered by webrx On 2021-08-27 11:21:32
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T8 {
    AtomicInteger num = new AtomicInteger(0);

    synchronized void work() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String n = Thread.currentThread().getName();
        System.out.printf("%s%d ", n, num.incrementAndGet());
        if ("C".equals(n)) {
            System.out.println();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        var t = new T8();
        while (true) {
            var tt1 = new Thread(t::work, "A");
            tt1.start();
            tt1.join();
            var tt2 = new Thread(t::work, "B");
            tt2.start();
            tt2.join();
            var tt3 = new Thread(t::work, "C");
            tt3.start();
            tt3.join();
        }


    }
}
